#!/bin/bash

# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Start the Dev Server after making sure we are running under a chroot.
. /usr/lib/crosutils/common.sh || exit 1

# Script must be run inside the chroot if not in 'always serve' mode.
if [[ "$1" != "--archive_dir" ]]; then
  restart_in_chroot_if_needed "$@"
fi

export DEFAULT_INSTALL_MASK

# Trap various signals (but do nothing about it).
trap : INT TERM QUIT

# Spawn devserver in the background, record its pid.
if [ -f /usr/lib/devserver/devserver.py ]; then
  python /usr/lib/devserver/devserver.py "$@" &
else
  python /usr/bin/devserver.py "$@" &
fi
devserver_pid=$!

# Wait for devserver to terminate. If we're interrupted by a signal, kill the
# devserver and wait for it to die.
wait
while [ $? -ge 128 ]; do
  kill ${devserver_pid}
  wait
done
